#include <iostream>
#include <vector>
const int maxn = 25+5;
using namespace std;
int N;
vector<int> B[maxn];

void Find(const int x, int &i, int &j){
    for (i = 0; i < N; ++i) {
        for (j = 0; j < B[i].size(); ++j) {
            if(B[i][j] == x) return ;
        }
    }
}

void Return(const int x){
    int i, j;
    Find(x, i, j);
    for (int k = j+1; k < B[i].size(); ++k) {
        int t = B[i][k];
        B[t].push_back(t);
    }
    B[i].resize(j+1);
}

void Pile(int x, int y){
    int xr, xc, yr, yc;
    Find(x, xr, xc); Find(y, yr, yc);
    for (int i=xc; i < B[xr].size(); ++i) {
        int t = B[xr][i];
        B[yr].push_back(t);
    }
    B[xr].resize(xc);
}

int main() {
    cin >> N;
    for (int i = 0; i < N; ++i) {
        B[i].push_back(i);
    }
    while(true){
        string s1, s2; int a, b;
        cin >> s1;
        if(s1 == "quit") break;
        cin >> a >> s2 >> b;

        int ar, ac, br, bc;
        Find(a, ar, ac); Find(b, br, bc);
        if(ar==br) continue;
        if(s1 == "move") Return(a);
        if(s2 == "onto") Return(b);
        Pile(a, b);
    }

    for (int i = 0; i < N; ++i) {
        cout << i << ":";
        for (int j : B[i]) {
            cout << " " << j;
        }
        cout << "\n";
    }
    return 0;
}
